/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.beans.Users;
import com.metier.UsersMetier;

/**
 *
 * @author Mamadou
 */
public class PanelConsultants extends javax.swing.JPanel {

	// Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boutonAjouter;
    private javax.swing.JButton boutonSupprimer;
    private javax.swing.JButton boutonImprimer;
    private javax.swing.JButton boutonModifier;
    private javax.swing.JButton boutonRechercher;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel panelBas;
    private javax.swing.JPanel panelBoutons;
    private javax.swing.JPanel panelHaut;
    private javax.swing.JTextField textRechercher;
    DefaultTableModel dtm;
	UsersMetier usersMetier;
	JTable tableListeUsers;
    // End of variables declaration//GEN-END:variables

    public PanelConsultants() {
    	usersMetier = new UsersMetier();
        initComponents();
    }

    
   
    private void initComponents() {

        panelHaut = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        panelBas = new javax.swing.JPanel();
        panelBoutons = new javax.swing.JPanel();
        boutonAjouter = new javax.swing.JButton();
        boutonModifier = new javax.swing.JButton();
        boutonSupprimer = new javax.swing.JButton();
        boutonImprimer = new javax.swing.JButton();
        boutonRechercher = new javax.swing.JButton();
        textRechercher = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(742, 450));
        setMinimumSize(new java.awt.Dimension(742, 450));
        setLayout(new java.awt.BorderLayout());

        panelHaut.setBackground(new java.awt.Color(223, 242, 255));
        panelHaut.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(102, 153, 255)));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photos/PhotoUsers1.jpg"))); // NOI18N

        javax.swing.GroupLayout panelHautLayout = new javax.swing.GroupLayout(panelHaut);
        panelHaut.setLayout(panelHautLayout);
        panelHautLayout.setHorizontalGroup(
            panelHautLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        panelHautLayout.setVerticalGroup(
            panelHautLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        add(panelHaut, java.awt.BorderLayout.PAGE_START);

        panelBas.setBackground(new java.awt.Color(223, 242, 255));
        panelBas.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(102, 153, 255)));
        panelBas.setMaximumSize(new java.awt.Dimension(742, 303));
        panelBas.setMinimumSize(new java.awt.Dimension(742, 303));
        panelBas.setPreferredSize(new java.awt.Dimension(742, 303));
        panelBas.setLayout(new java.awt.BorderLayout());
      //  add(panelBas, java.awt.BorderLayout.SOUTH);

        panelBoutons.setBackground(new java.awt.Color(223, 242, 255));
        panelBoutons.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(102, 153, 255)));
        panelBoutons.setForeground(new java.awt.Color(102, 153, 255));
        panelBoutons.setMaximumSize(new java.awt.Dimension(742, 43));
        panelBoutons.setMinimumSize(new java.awt.Dimension(742, 43));

        boutonAjouter.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photos/add16.png"))); // NOI18N

        boutonModifier.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photos/modifier.gif"))); // NOI18N

        boutonSupprimer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photos/supprimer.gif"))); // NOI18N

        boutonImprimer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photos/imprimer.gif"))); // NOI18N

        boutonRechercher.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/photos/supprim.jpg"))); // NOI18N

        textRechercher.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
        textRechercher.setText("rechercher");

        jLabel2.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(191, 36, 94));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Liste des consultants");

        javax.swing.GroupLayout panelBoutonsLayout = new javax.swing.GroupLayout(panelBoutons);
        panelBoutons.setLayout(panelBoutonsLayout);
        panelBoutonsLayout.setHorizontalGroup(
            panelBoutonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBoutonsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(boutonAjouter, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boutonModifier, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boutonSupprimer, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boutonImprimer, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boutonRechercher, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(textRechercher, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(207, Short.MAX_VALUE))
        );
        panelBoutonsLayout.setVerticalGroup(
            panelBoutonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBoutonsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelBoutonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelBoutonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(boutonAjouter, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(boutonSupprimer)
                        .addComponent(boutonModifier, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(boutonImprimer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(boutonRechercher, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(textRechercher, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        add(panelBoutons, java.awt.BorderLayout.CENTER);
        
		tableauListUsers();// on fait appel à cette méthode pour le chargement
		// du tableau
      panelBas.add(new JScrollPane(tableListeUsers), BorderLayout.CENTER);
      add(panelBas, BorderLayout.SOUTH);

boutonSupprimer.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
testDeleteUsers();

}
});
boutonAjouter.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
jLabel2.setVisible(false);
boutonImprimer.setEnabled(false);
boutonModifier.setEnabled(false);
boutonRechercher.setEnabled(false);
boutonSupprimer.setEnabled(false);

panelBas.removeAll();
PanelAjoutConsultant panelAjouterConsultant = new PanelAjoutConsultant();
add(panelAjouterConsultant, BorderLayout.SOUTH);
validate();



}
});
}


// méthode qui permet de charger les users dans un JTABLE

public void tableauListUsers() {

String[] entetes = { "NOM", "Prenom", "Login", "E-Mail", "Telephone",
"Profil","Adresse" };

List<Users> listUsers = usersMetier.chargerAllUsers();
int nombreLigne = listUsers.size();
System.out.println("taille de la liste" + listUsers.size());
Object data[][] = new Object[nombreLigne][7];

// On parcoure la liste pour recuperer les users et remplir le tableau
// d'objet
for (int i = 0; i < nombreLigne; i++) {
Users users = new Users();
users = listUsers.get(i);

data[i][0] = users.getUsrPrenom();
data[i][1] = users.getUsrNom();

data[i][2] = users.getUsrLogin();
data[i][3] = users.getUsrEmail();

data[i][4] = users.getUsrPhone();
data[i][5] = users.getUsrProfil();
data[i][6]=users.getUsrAdresse();

}

dtm = new DefaultTableModel(data, entetes);
tableListeUsers = new JTable(dtm);

}

// méhtho de teste pour Supprimer un users

public void testDeleteUsers() {

// on recupére le nombre de ligne selectionné pour la suppression
tableauListUsers();
final int[] selection = tableListeUsers.getSelectedRows();
System.out.println("voici la taille selectionné " + selection.length);
// test pour voir si la selection est vide
if (selection.length == 0) {
JOptionPane.showMessageDialog(null,
"veuillez selectionner un consultant d'abord!!!!");

} else {// si la selection n'est pas vide
// int taille = selection.length;

for (int i = 0; i < selection.length; i++) {
int index = selection[i];

String userLogin = (String) tableListeUsers
	.getValueAt(index, 1);

Users utilisateur = usersMetier.findUserByLogin(userLogin);
usersMetier.testDelete(utilisateur);
dtm.removeRow(index);

}

}
}

public JTable getTableListeUsers() {
return tableListeUsers;
}

public void setTableListeUsers(JTable tableListeUsers) {
this.tableListeUsers = tableListeUsers;
}
         
}